﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web
{
    public partial class register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(register));
        }

        //验证验证码是否正确
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string VerificationCode(string code)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            string verCode = Session["VerificationCode"] == null ? "" : Session["VerificationCode"].ToString();
            if (verCode.ToLower()==code.ToLower())
            {
                jsonResult.Add("isreload",true);
            }
            else
            {
                jsonResult.Add("isreload", false);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }

        //注册
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public string UerRegister(string account,string pwd)
        {
            Dictionary<string, object> jsonResult = new Dictionary<string, object>();
            try
            {
                int count = ClientInfoBll.Register(CommonBll.InputText(account, 50), EncryptHelper.EncryptString(pwd),  CommonConst.DB_Name);
                if (count == 1)
                {
//                    jsonResult.Add("isreload", true);
                    //登录
                    DataTable table = ClientInfoBll.ClientLogin(CommonBll.InputText(account, 50), EncryptHelper.EncryptString(pwd),  CommonConst.DB_Name);
                    if (table.Rows.Count > 0)
                    {
                        //判断当天是否已经登录过
                        if (table.Rows[0]["LastLoginTime"] == DBNull.Value || (Convert.ToDateTime(table.Rows[0]["LastLoginTime"].ToString()) - DateTime.Now).Days != 0)
                        {
                            //登录时积分+5并更新等级
                            ClientInfoBll.UpdateClientPoints(table.Rows[0]["Client_Id"].ToString(), CommonConst.gLoginPoints, "+",  CommonConst.DB_Name);
                            //更新最后登录时间
                            ClientInfoBll.UpateLastLoginTime(table.Rows[0]["Client_Id"].ToString(),  CommonConst.DB_Name);
                        }
                        

                        jsonResult.Add("isreload", true);
                        var info = new UserInfo
                        {
                            Email = table.Rows[0]["User_Email"] == DBNull.Value ? "" : table.Rows[0]["User_Email"].ToString(),
                            Id = table.Rows[0]["Client_Id"].ToString(),
                            RelName = table.Rows[0]["Client_Name"] == DBNull.Value ? "" : table.Rows[0]["Client_Name"].ToString(),
                            Status = table.Rows[0]["User_Status"].ToString(),
                            UName = table.Rows[0]["Client_Account"].ToString()
                        };
                        info.Computername = HttpContext.Current.Application["computername"].ToString();
                        info.Computerip = HttpContext.Current.Application["computerip"].ToString();
                        WebMasterCookie.AddCookie(info);

                        Dictionary<string, object> dicLoginLog = new Dictionary<string, object>();
                        dicLoginLog.Add("user_id", info.Id);
                        dicLoginLog.Add("computername", info.Computername);
                        dicLoginLog.Add("computerip", info.Computerip);
                        LogLoginBll.AddLoginLog(dicLoginLog, CommonConst.DB_Name);
                        jsonResult.Add("msg", "/member/settings.html");
                    }
                    else
                    {
                        jsonResult.Clear();
                        jsonResult.Add("isreload", false);
                        jsonResult.Add("msg", "用户名或密码错误！");
                    }
                }
                else
                {
                    jsonResult.Add("isreload", false);
                    if (count == -1)
                    {
                        jsonResult.Add("msg", "用户名已经存在");
                    }
                    else
                    {
                        jsonResult.Add("msg", "注册失败");
                    }
                }
            }
            catch (Exception ex)
            {
                jsonResult.Add("isreload", false);
                jsonResult.Add("msg",ex.Message);
            }
            return "[" + JsonHelper.ObjectToJSON(jsonResult) + "]";
        }
    }
}
